package org.apache.lucene.distributed.index;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.io.Serializable;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.distributed.index.IndexService.IndexVersion;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.HitCollector;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searchable;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.Weight;
import org.apache.lucene.search.highlight.Encoder;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.highlight.TextFragment;
import org.apache.lucene.util.OpenBitSet;

public interface SearchableService extends Searchable {
  public IndexVersion getIndexVersion() throws Exception;
  public Document[] docs(int[] docs, FieldSelector fieldSelector) throws CorruptIndexException, IOException;
  
  public static class Search implements Serializable {
    public final Weight weight;
    public final Sort sort;
    public final int start;
    public final int total;
    public final FacetKey[] facetKeys;
    public final HighlightKey[] highlightKeys;
    
    public Search(Weight weight, Sort sort, int start, int total, FacetKey[] facetKeys, HighlightKey[] highlightKeys) {
      this.weight = weight;
      this.sort = sort;
      this.start = start;
      this.total = total;
      this.facetKeys = facetKeys;
      this.highlightKeys = highlightKeys;
    }
  }
  
  public static class HighlightKey implements Serializable {
    public final String field;
    public final Analyzer analyzer;
    public final Fragmenter fragmenter;
    public final Scorer scorer;
    public final Formatter formatter;
    public final Encoder encoder;
    public final int numFragments;
    
    public HighlightKey(String field, Analyzer analyzer, Fragmenter fragmenter, Scorer scorer, Formatter formatter, Encoder encoder,
        int numFragments) {
      this.field = field;
      this.analyzer = analyzer;
      this.fragmenter = fragmenter;
      this.scorer = scorer;
      this.formatter = formatter;
      this.encoder = encoder;
      this.numFragments = numFragments;
    }
  }
  
  public static class Higlight {
    public final TextFragment[] textFragments;

    public Higlight(TextFragment[] textFragments) {
      this.textFragments = textFragments;
    }
  }
  
  public abstract static class OpenBitSetKey implements Serializable {
    public abstract OpenBitSet createBitSet(IndexSearcher searcher) throws Exception;
  }
  
  public static class QueryBitSetKey extends OpenBitSetKey {
    Query query;
    
    public QueryBitSetKey(Query query) {
      this.query = query;
    }

    public OpenBitSet createBitSet(IndexSearcher searcher) throws Exception {
      final OpenBitSet bitSet = new OpenBitSet(searcher.maxDoc());
      searcher.search(query, new HitCollector() {
        public void collect(int doc, float score) {
          bitSet.fastSet(doc);
        }
      });
      return bitSet;
    }
  }
  
  public static abstract class FacetKey implements Serializable {
    public abstract Facet[] createFacets(IndexSearcher searcher) throws Exception;
  }
  
  public static class QueryFacetKey extends FacetKey {
    public final Query query;
    public final Query fq;

    public QueryFacetKey(Query query, Query fq) {
      this.query = query;
      this.fq = fq;
    }
    
    public Facet[] createFacets(IndexSearcher searcher) throws Exception {
      QueryBitSetKey key1 = new QueryBitSetKey(query);
      QueryBitSetKey key2 = new QueryBitSetKey(fq);
      OpenBitSet b1 = null;
      OpenBitSet b2 = null;
      int count = (int)OpenBitSet.intersectionCount(b1, b2);
      return new Facet[] {new Facet(this, count)};
    }
  }
  
  public static class Facet implements Serializable {
    public final FacetKey facetKey;
    public final int count;
    
    public Facet(FacetKey facetKey, int count) {
      this.facetKey = facetKey;
      this.count = count;
    }
  }
}
